package com.thinksns.sociax.t4.android.gift;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.ThinksnsAbscractActivity;
import com.thinksns.sociax.t4.android.popupwindow.CustomPopupWindow;
import com.thinksns.sociax.t4.android.presenter.BindAccountPresenter;
import com.thinksns.sociax.t4.android.view.BindAccountView;
import com.thinksns.sociax.t4.unit.CharactorHandler;
import com.thinksns.sociax.t4.unit.UiUtils;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by jess on 16/6/2.
 */
public class BindAccountActivity extends ThinksnsAbscractActivity implements BindAccountView {
    public static final int ACCOUNT_WEICHAT = 2;
    public static final int ACCOUNT_ALIPAY = 1;
    private CustomPopupWindow mPopupWindow;
    private CustomPopupWindow mUnbindPop;
    private int mCurrentAccount;
    private View mContentView;
    private View mUnbindContentView;
    private boolean isWeiChatBind;
    private boolean isAlipayBind;

    private BindAccountPresenter mPresenter;

    @Nullable
    @BindView(R.id.bt_bind_account_alipay)
    Button mAlipayBT;
    @Nullable
    @BindView(R.id.bt_bind_account_wechat)
    Button mWeChatBT;


    private ProgressDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreateNoTitle(savedInstanceState);
        initData();
    }

    protected void initData() {
        mPresenter = new BindAccountPresenter(this);
        isAlipayBind = getIntent().getBooleanExtra("alipay", false);
        isWeiChatBind = getIntent().getBooleanExtra("weichat", false);//获得绑定账号的信息
        setBindStatus();
        initDialog();
    }

    /**
     * 设置绑定状态
     */
    public void setBindStatus() {
        mAlipayBT.setVisibility(isAlipayBind ? View.VISIBLE : View.GONE);
        mWeChatBT.setVisibility(isWeiChatBind ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setLoadingMessage(String message) {
        mLoadingDialog.setMessage(message);
    }

    @Override
    public void setWeiChatBindStatus(boolean isBind) {
        isWeiChatBind = isBind;
    }

    @Override
    public void setAlipayBindStatus(boolean isBind) {
        isAlipayBind = isBind;
    }

    private void initDialog() {
        mLoadingDialog = new ProgressDialog(this);
        mLoadingDialog.setMessage(getString(R.string.str_binding));
        mLoadingDialog.setCanceledOnTouchOutside(false);

        mPopupWindow = CustomPopupWindow.newInstance(LayoutInflater.from(this)
                        .inflate(R.layout.popup_top_up, null), null
                , new CustomPopupWindow.CustomPopupWindowListener() {
                    @Override
                    public void initPopupView(View contentView) {
                        mContentView = contentView;
                        final EditText mEditText = UiUtils.findViewByName(contentView, "et_top_up_edit");
                        mEditText.setFilters(new InputFilter[]{CharactorHandler.emojiFilter});//emoji过滤

                        ((AutoLinearLayout) UiUtils.findViewByName(contentView, "ll_top_up_edit"))
                                .setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        mEditText.requestFocus();
                                    }
                                });
                        contentView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mPopupWindow.dismiss();
                            }
                        });

                        ((Button) UiUtils.findViewByName(contentView, "bt_top_up_cancel")).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mPopupWindow.dismiss();
                            }
                        });

                        ((Button) UiUtils.findViewByName(contentView, "bt_top_up_confirm")).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mPopupWindow.dismiss();
                                bindAccount(mEditText);//绑定账号
                            }
                        });


                    }
                }, false);
        mPopupWindow.setFocusable(true);
        // 设置弹出窗体需要软键盘，
        mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        // 避免软键盘挡住弹窗
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mUnbindPop = CustomPopupWindow.newInstance(LayoutInflater.from(this)
                .inflate(R.layout.popup_top_up, null), null, new CustomPopupWindow.CustomPopupWindowListener() {
            @Override
            public void initPopupView(View contentView) {
                mUnbindContentView = contentView;
                final EditText mEditText = UiUtils.findViewByName(contentView, "et_top_up_edit");
                mEditText.setFilters(new InputFilter[]{CharactorHandler.emojiFilter});//emoji过滤
                mEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ((AutoLinearLayout) UiUtils.findViewByName(contentView, "ll_top_up_edit"))
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mEditText.requestFocus();
                            }
                        });

                contentView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mUnbindPop.dismiss();
                    }
                });

                mEditText.setHint("请输入登陆密码");
                ((Button) UiUtils.findViewByName(contentView, "bt_top_up_cancel")).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mUnbindPop.dismiss();
                    }
                });

                ((Button) UiUtils.findViewByName(contentView, "bt_top_up_confirm")).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mUnbindPop.dismiss();

                    }
                });
            }
        }, false);
    }

    private void unbindAccount() {
        mPresenter.unbindWallet(mCurrentAccount);
    }

    private void bindAccount(EditText mEditText) {
        if (TextUtils.isEmpty(mEditText.getText().toString().trim())) {
            showMessage("账户不能为空~");
            return;//如果没有输入则提示
        }
        mPresenter.bindWallet(mEditText.getText().toString(), mCurrentAccount);//绑定账号
    }

    @Nullable
    @OnClick({R.id.tv_title_left, R.id.uv_bind_zhifubao, R.id.uv_bind_weixin
            , R.id.bt_bind_account_alipay, R.id.bt_bind_account_wechat})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_title_left:
                killMyself();
                break;
            case R.id.uv_bind_zhifubao:
                bindAlipay();
                break;
            case R.id.uv_bind_weixin:
                bindWeichat();
                break;
            case R.id.bt_bind_account_alipay:
                mCurrentAccount = ACCOUNT_ALIPAY;
                unbindConvert();
                unbindAccount();//解绑账号
                break;
            case R.id.bt_bind_account_wechat:
                mCurrentAccount = ACCOUNT_WEICHAT;
                unbindConvert();
                unbindAccount();//解绑账号
                break;
        }
    }

    /**
     * 绑定微信
     */
    private void bindWeichat() {
        mCurrentAccount = ACCOUNT_WEICHAT;//区分点击的账号:0为支付宝,1为微信
        convert();
        //如果点击的是支付宝并且支付宝已经绑定,或者点击的微信并且微信已经绑定,则选择此账号提现
        if (mCurrentAccount == ACCOUNT_ALIPAY && isAlipayBind
                || mCurrentAccount == ACCOUNT_WEICHAT && isWeiChatBind) {
            killMyself();
        } else {//否则绑定账号
            mPopupWindow.show();
        }
    }

    /**
     * 绑定支付宝
     */
    private void bindAlipay() {
        mCurrentAccount = ACCOUNT_ALIPAY;//区分点击的账号:1为支付宝,2为微信
        convert();
        //如果点击的是支付宝并且支付宝已经绑定,或者点击的微信并且微信已经绑定,则选择此账号提现
        if (mCurrentAccount == ACCOUNT_ALIPAY && isAlipayBind
                || mCurrentAccount == ACCOUNT_WEICHAT && isWeiChatBind) {
            killMyself();
        } else {//否则绑定账号
            mPopupWindow.show();
        }
    }

    /**
     * 转换解除绑定的标题
     */
    private void unbindConvert() {
//        if (mUnbindContentView == null || bindType !=0) return;
//        EditText editText = UiUtils.findViewByName(mUnbindContentView, "et_top_up_edit");
//        //请求获得焦点
//        editText.requestFocus();
//        ((TextView) UiUtils.findViewByName(mUnbindContentView, "tv_top_up_title")).setText(
//                mUnbindAccount.equals(WALLET_TYPE_ALIPAY) ? "支付宝解除绑定" : "微信解除绑定");
    }

    /**
     * 转换标题
     */
    private void convert() {
        if (mContentView == null) return;
        EditText editText = UiUtils.findViewByName(mContentView, "et_top_up_edit");
        //请求获得焦点
        editText.requestFocus();
        ((TextView) UiUtils.findViewByName(mContentView, "tv_top_up_title")).setText(
                mCurrentAccount == ACCOUNT_ALIPAY ? "支付宝账号绑定" : "微信账号绑定");

        editText.setHint(
                mCurrentAccount == ACCOUNT_ALIPAY ? "请输入支付宝账号" : "请输入微信账号"
        );

    }

    @Override
    public void showLoading() {
        mLoadingDialog.show();
    }

    @Override
    public void hideLoading() {
        mLoadingDialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(Intent intent) {
        startActivity(intent);
    }

    @Override
    public String getTitleCenter() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_account;
    }

    @Override
    public void killMyself() {
        super.onBackPressed();
    }

    @Override
    public void onBackPressed() {
        killMyself();
    }
}
